<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList,teamCservlets.ApplicationBean,teamCservlets.DefectBean,teamCservlets.PersonBean" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Team C Defect System</title>
</head>

<%-- 
First, get all of the session and request data this jsp requires.  Make sure all
the data is available before building the display, to avoid any null pointer exceptions.
--%>
<jsp:useBean id="user" type="teamCservlets.PersonBean" scope="session"/>
<jsp:useBean id="currentDefect" type="teamCservlets.DefectBean" scope="session"/>
<%
String output;
if ((user == null) || (user.getId() == -1)) { 
	// No valid user object in the session, so the user must login
	response.sendRedirect("login.jsp");
}
%>
<%
ApplicationBean defApplication = (ApplicationBean) request.getAttribute("defectApplication");
DefectBean defect = (DefectBean) session.getAttribute("currentDefect");
int assigneeID = defect.getAssigneeID();
ArrayList<PersonBean> people = (ArrayList<PersonBean>) session.getAttribute("personList");

String defectSummary = defect.getSummary();
String defectDescription = defect.getDetailedDescription();
String defectID = String.valueOf(defect.getDefectID());
int submitterID = defect.getSubmitterID();
String submitterName = null;
for (PersonBean person : people) {
	if (person.getId() == submitterID) {
		submitterName = person.getFirstAndLastName();
	}
}

boolean error = false;

// The "user" attribute identifies the currently logged-in user, if any.  If it's not a valid
// user, force the user to login.
if ((user == null) || (user.getId() == -1)) { 
	// No valid user object in the session, so the user must login
	response.sendRedirect("login.jsp");
	error = true;
} 
// The defect attribute is a DefectBean which might have some fields
// already filled in with values, which will be used on the display.
if (defect == null) { 
	out.println("defect is null");
	error = true;
} 
// Application with the defect.
if (defApplication == null) {
	out.println("defApplication is null");
	error = true;
}
// List of users, used to populate the Assignee combo box.
if (people == null) {
	out.println("people is null");
	error = true;
}
// Name of the submitter
if (submitterName == null) {
	out.println("submitterName is null");
	error = true;
}
if (!error) {
%>

<body BGCOLOR="#FDF5E6">
	<H1 ALIGN="CENTER">Team C Defect System</H1>
	<p style="font-size:150%">Defect Details</p>

<form action="defectforms">
	<table width="100%">
	<tr><td>You may update this defect's info, close it, or send an email.</td>
		<td align="right"><input type="submit" name="btnCloseDefect" value="Close Defect">
			<input type="submit" name="btnEmailDefect" value="Send Email">
			<input type="submit" name="btnGoToHomepage" value="Back to Defect List"></td>
	</tr>
	</table>
</form>


	<form action="defectactions">
	<fieldset><legend>Update defect:</legend>
	<table>
	<tr>
	<td>Defect ID:</td><td><%=defectID%></td>
	</tr>
	<tr>
	<td>Submitted by:</td><td><%=submitterName%></td>
	</tr>
	<tr>
	<td>Application:</td><td><%=defApplication.getApplicationName()%></td>
	</tr>
	<tr>

	<td>Priority:</td><td><select name="chosenPriority" width="100%">
	<%	
	if(defect.getPriority().equals("LOW")) {	
	%>
		<option selected value="LOW"> Low
	<% } else {
	%>
		<option value="LOW"> Low
	<%
	}
	%>

	<%	
	if(defect.getPriority().equals("MEDIUM")) {	
	%>
		<option selected value="MEDIUM"> Medium
	<% } else {
	%>
		<option value="MEDIUM"> Medium
	<%
	}
	%>

	<%	
	if(defect.getPriority().equals("HIGH")) {	
	%>
		<option selected value="HIGH"> High
	<% } else {
	%>
		<option value="HIGH"> High
	<%
	}
	%>
	
	</select></td>
	</tr>
	<tr>

<%-- Display the person indicated by defectToDisplay.assigneeID as the current Assignee, 
and allow the user to select a different person from the list. --%>

	<td>Assignee:</td><td><select name="chosenAssignee" width="100%">
	
	<%-- Populate the Assignee combo box.  This inserts the person's ID 
	into the "value" and the person's name into the displayed list item.  When the user selects
	an assignee and sends the form back to the servlet, the "value" will be retrieved, and this
	will be the person's ID. --%>
	
	<% if(assigneeID < 0) {
	%>
		<option selected value="<%=assigneeID%>"> (none)
	<% }
	%>

	
	<%
	for (PersonBean person : people) {
		if(assigneeID != person.getId()) {
	%>
		<option  value="<%= person.getId() %>"><%= person.getFirstName() + " " + person.getLastName()%>
	<%
		} else {
	%>
		<option selected value="<%= person.getId() %>"><%= person.getFirstName() + " " + person.getLastName()%>
	<%	}
	}	
	%>
	
	</select></td>
	</tr>
	<tr>
	<td>Summary:</td><td><label id="lblSummary"></label></td>
	</tr>
	<td align="right" colspan="2" style="width:80"><textarea name="textSummary" rows=1 cols=60 ><%=defectSummary %></textarea></td>
	<tr>
	<td>Description (describe the problem and steps to recreate it):</td>
	</tr>
	<tr>
	<td align="right" colspan="2" style="width:80"><textarea name="textDescription" rows=10 cols=60 ><%=defectDescription %></textarea></td>
	</tr>
	
	<tr>
		
		<td colspan="2" align="left">
		
		<input type="submit" name="btnStoreUpdatedDefect" value="Save">
		<input type="submit" name="btnGoToHomepage" value="Cancel">
	</table>
	</fieldset>
	</form>

</body>
<% } // end if (!error)
%>
</html>